﻿/*
 *
 * @author
 * @version
 */
import com.gamemeal.physics.IForce;
import com.gamemeal.physics.Point3;

class com.gamemeal.physics.Particle{
	private var forceArray:Array;
	public var d:Point3,vi:Point3,a:Point3,f:Point3;
	public var m:Number;
	public var r:Number;
	private var vf:Point3;

	//rotational Motion
	public var θ:Number,ω:Number,α:Number,τ:Number;
	public var inertia:Number;
	private var ωf:Number;

	private var ot:Number,nt:Number,dt:Number;


	public function Particle(){
		forceArray = new Array();
		m=1;
		r=0;
		f = new Point3(0,0,0);
		d=new Point3(0,0,0);//distance,position
		vi=new Point3(0,0,0);//inital velocity
		vf=new Point3(0,0,0);//final velocity
		a=new Point3(0,0,0);//inital accelration

		inertia = 1;
		τ=0;		
		θ=0;
		ω=0;
		ωf=0;
		α=0;

		ot=getTimer();//start time
	}

	public function update(dt){
		if(dt==undefined){
			//check the delta time;
			nt=getTimer();
			dt=(nt-ot)/1000;
			ot=nt;
		}

		//all physics affect this particle;
		applyForce(dt);
		
		//f=ma;
		//a=f/m;
		a.x = f.x/m;
		a.y = f.y/m;
//		a.z = f.z/m;

		//vf=vi+at, final velocity = inital velocity + accelration * time
		//df=di+dv
		vf.x = vi.x+a.x*dt;
		vf.y = vi.y+a.y*dt;
//		vf.z = vi.z+a.z*dt;

		//move the point
		d.x += (vf.x+vi.x)*dt/2;
		d.y += (vf.y+vi.y)*dt/2;
//		d.z += (vf.z+vi.z)*dt/2;

		//completed and update properties
		vi.x = vf.x;
		vi.y = vf.y;
//		vi.z = vf.z;
		f.x = 0;
		f.y = 0;
//		f.z = 0;
		
		//rotational motion
		ωf = ω+α*dt;
		θ += (ωf+ω)*dt/2;
		ω = ωf;
		ωf=0;
		τ = 0;
		α = 0;
	}

	private function applyForce(t){
		var i=0;
		for (i=0;i<forceArray.length;i++){
			forceArray[i].apply(this,t);
		}
	}

	public function addForce(f:IForce){
		forceArray.push(f);
	}

	public function getForce():Array{
		return forceArray;
	}
	
	public function getMomentum():Point3{
		return Point3.scale(vi,m);
	}
}